package com.brzjomo.embraceofthevoid;

import com.brzjomo.embraceofthevoid.init.ModBlocks;
import com.brzjomo.embraceofthevoid.init.ModItemGroups;
import com.brzjomo.embraceofthevoid.init.ModItems;
import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint;
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricBlockLootTableProvider;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricModelProvider;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricRecipeProvider;
import net.minecraft.data.client.BlockStateModelGenerator;
import net.minecraft.data.client.ItemModelGenerator;
import net.minecraft.data.client.Models;
import net.minecraft.data.server.recipe.RecipeJsonProvider;
import net.minecraft.data.server.recipe.ShapedRecipeJsonBuilder;
import net.minecraft.item.Items;
import net.minecraft.recipe.book.RecipeCategory;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;

import java.util.function.Consumer;

public class EmbraceOfTheVoidDataGenerator implements DataGeneratorEntrypoint {
    @Override
    public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) {
        fabricDataGenerator.createPack().addProvider(ModModelGenerator::new);
        fabricDataGenerator.createPack().addProvider(ModRecipeGenerator::new);
        fabricDataGenerator.createPack().addProvider(ModBlockLootTables::new);
        fabricDataGenerator.createPack().addProvider(ModEnglishLangProvider::new);
    }

    //Model generator
    private static class ModModelGenerator extends FabricModelProvider {

        public ModModelGenerator(FabricDataOutput output) {
            super(output);
        }

        @Override
        public void generateBlockStateModels(BlockStateModelGenerator blockStateModelGenerator) {
            blockStateModelGenerator.registerNorthDefaultHorizontalRotation(ModBlocks.VILLAGER_ALTAR);
        }

        @Override
        public void generateItemModels(ItemModelGenerator itemModelGenerator) {
            itemModelGenerator.register(ModItems.NBT_STICK, Models.GENERATED);
            itemModelGenerator.register(ModItems.NIGHTFALL_TELEPORT_GEM, Models.GENERATED);
            itemModelGenerator.register(ModItems.BLESS_TELEPORT_GEM, Models.GENERATED);
            itemModelGenerator.register(ModItems.TELEPORT_GEM_CORE, Models.GENERATED);
        }
    }

    //Recipe generator
    private static class ModRecipeGenerator extends FabricRecipeProvider {
        private ModRecipeGenerator(FabricDataOutput output) {
            super(output);
        }

        @Override
        public void generate(Consumer<RecipeJsonProvider> exporter) {
            ShapedRecipeJsonBuilder.create(RecipeCategory.MISC, ModItems.NIGHTFALL_TELEPORT_GEM, 1)
                    .input('A', ModItems.TELEPORT_GEM_CORE)
                    .input('B', Items.CAMPFIRE)
                    .pattern("AB")
                    .criterion(FabricRecipeProvider.hasItem(ModItems.TELEPORT_GEM_CORE), FabricRecipeProvider.conditionsFromItem(ModItems.TELEPORT_GEM_CORE))
                    .criterion(FabricRecipeProvider.hasItem(Items.CAMPFIRE), FabricRecipeProvider.conditionsFromItem(Items.CAMPFIRE))
                    .offerTo(exporter);
            ShapedRecipeJsonBuilder.create(RecipeCategory.MISC, ModItems.BLESS_TELEPORT_GEM, 1)
                    .input('A', ModItems.TELEPORT_GEM_CORE)
                    .input('B', Items.ENDER_PEARL)
                    .pattern("AB")
                    .criterion(FabricRecipeProvider.hasItem(ModItems.TELEPORT_GEM_CORE), FabricRecipeProvider.conditionsFromItem(ModItems.TELEPORT_GEM_CORE))
                    .criterion(FabricRecipeProvider.hasItem(Items.ENDER_PEARL), FabricRecipeProvider.conditionsFromItem(Items.ENDER_PEARL))
                    .offerTo(exporter);
            ShapedRecipeJsonBuilder.create(RecipeCategory.MISC, ModItems.VILLAGER_ALTAR, 1)
                    .input('A', ModItems.TELEPORT_GEM_CORE)
                    .input('B', Items.EMERALD)
                    .input('C', Items.STONE_BRICKS)
                    .pattern(" B ")
                    .pattern("BAB")
                    .pattern("CCC")
                    .criterion(FabricRecipeProvider.hasItem(ModItems.TELEPORT_GEM_CORE), FabricRecipeProvider.conditionsFromItem(ModItems.TELEPORT_GEM_CORE))
                    .criterion(FabricRecipeProvider.hasItem(Items.EMERALD), FabricRecipeProvider.conditionsFromItem(Items.EMERALD))
                    .criterion(FabricRecipeProvider.hasItem(Items.STONE_BRICKS), FabricRecipeProvider.conditionsFromItem(Items.STONE_BRICKS))
                    .offerTo(exporter);
        }
    }

    //Loot table generator
    private static class ModBlockLootTables extends FabricBlockLootTableProvider {
        public ModBlockLootTables(FabricDataOutput output) {
            super(output);
        }

        @Override
        public void generate() {
            addDrop(ModBlocks.VILLAGER_ALTAR, drops(ModItems.VILLAGER_ALTAR));
        }
    }

    //Lang generator
    private static class ModEnglishLangProvider extends FabricLanguageProvider {
        private ModEnglishLangProvider(FabricDataOutput output) {
            // Specifying en_us is optional, by default is en_us.
            super(output, "en_us");
        }

        @Override
        public void generateTranslations(TranslationBuilder translationBuilder) {
            //Key prefix
            String ItemPrefix = "item." + EmbraceOfTheVoid.MOD_ID + ".";
            String BlockPrefix = "block." + EmbraceOfTheVoid.MOD_ID + ".";
            //Item groups
            translationBuilder.add(RegistryKey.of(RegistryKeys.ITEM_GROUP, ModItemGroups.getId(ModItemGroups.MOD_GROUP_MAIN)), "Embrace Of The Void");
            //General
            translationBuilder.add("dimension.embraceofthevoid.overworld", "Overworld");
            translationBuilder.add("dimension.embraceofthevoid.the_nether", "The Nether");
            translationBuilder.add("dimension.embraceofthevoid.the_end", "The End");
            translationBuilder.add("item.embraceofthevoid.teleport_gem.recordedPosition", "Recorded Position : [ %s ]");
            translationBuilder.add("item.embraceofthevoid.teleport_gem.noRecord", "Coordinates not yet recorded");
            translationBuilder.add("item.embraceofthevoid.teleport_gem.recordedDimension", "Recorded Dimension : [ %s ]");
            translationBuilder.add("item.embraceofthevoid.teleport_gem.NBT", "NBT : [ %s ]");
            //Items
            translationBuilder.add(ItemPrefix + ModItems.NIGHTFALL_TELEPORT_GEM, "Nightfall Teleport Gem - [ %s ]");
            translationBuilder.add(ItemPrefix + ModItems.NIGHTFALL_TELEPORT_GEM + ".targetTime", "Teleport Time : [ %s ]");
            translationBuilder.add(ItemPrefix + ModItems.NIGHTFALL_TELEPORT_GEM + ".currentTime", "Current Time : [ %s ]");
            translationBuilder.add(ItemPrefix + ModItems.NIGHTFALL_TELEPORT_GEM + ".description", "Stealth right click to record the coordinates, hold the gem at dusk, and teleport home");
            translationBuilder.add(ItemPrefix + ModItems.NIGHTFALL_TELEPORT_GEM + ".description.2", "At dusk, thoughts will guide travelers home. Looking at it is like seeing home.");
            translationBuilder.add(ItemPrefix + ModItems.BLESS_TELEPORT_GEM, "Bless Teleport Gem");
            translationBuilder.add(ItemPrefix + ModItems.BLESS_TELEPORT_GEM + ".description", "Teleport player away from danger when life is at stake");
            translationBuilder.add(ItemPrefix + ModItems.BLESS_TELEPORT_GEM + ".description.2", "Fortunately or unfortunately, time reveals the answer.");
            translationBuilder.add(ItemPrefix + ModItems.TELEPORT_GEM_CORE, "Teleport Gem Core");
            translationBuilder.add(ItemPrefix + ModItems.TELEPORT_GEM_CORE + ".description", "The core of teleport gems");
            translationBuilder.add(ItemPrefix + ModItems.TELEPORT_GEM_CORE + ".description.2", "It vibrates every now and then and seems unstable");
            translationBuilder.add(ItemPrefix + ModItems.NBT_STICK, "NBT Stick");
            translationBuilder.add(BlockPrefix + ModItems.VILLAGER_ALTAR, "Villager Alter");
            translationBuilder.add(BlockPrefix + ModItems.VILLAGER_ALTAR + ".description", "1");
            translationBuilder.add(BlockPrefix + ModItems.VILLAGER_ALTAR + ".description.2", "2");
            //Messages
            translationBuilder.add("message.embraceofthevoid.near_dusk", "Dusk is approaching");
        }
    }
}
